package com.tonglin.springboot.controller;

import com.tonglin.springboot.po.*;
import com.tonglin.springboot.service.AdminService;
import com.tonglin.springboot.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class AdminController {

    @Autowired
    AdminService adminService;

    @Autowired
    BookService bookService;

    @RequestMapping("home")
    public String home(){



        return "home";
    }

    @RequestMapping("adminLogin")
    public String adminLogin(String adminName, String passWord, Model model){

        Status status = adminService.adminLogin(adminName,passWord);

        System.out.println(adminName);
        System.out.println(passWord);
        if(status.isCode()){

            // model.addAttribute("msg",loginService.login(userName,passWord).getMsg());
            model.addAttribute("mm",status);
            model.addAttribute("name",adminName);
            return "adminmsg";

        }else {
            model.addAttribute("msg",status.getMsg());

            return "adminLoginPage";
        }

    }
    @RequestMapping("manageBooks")
    public String manageBooks(){


  return "manageBooksHtml";
    }

    //图书下架页面跳转
    @RequestMapping("bookSJ")
    public  String bookSJ(Model model){

        List<Book> bookList = bookService.showBooks();
        if(bookList!=null){
            model.addAttribute("books",bookList);
        }

        return "bookShangjia";
    }

    //图书下架，接受数据
    @RequestMapping("bookXj")
    public String  bookXj(String bookName,Model model){

     Status status = adminService.xiaJiaBook(bookName);

         model.addAttribute("msg",status.getMsg());
         return "xiajiaMsg";



    }
    //图书上架，接受数据
    @RequestMapping("bookSj")
    public String bookSj(String bookName,String bookDes,String bookNumber,String bookId,Model model) {

        Integer bookNumber1 = Integer.parseInt(bookNumber);
        Integer bookId1 = Integer.parseInt(bookId);

        Status status = adminService.shangJiaBook(bookName, bookId1, bookDes, bookNumber1);


        model.addAttribute("msg", status.getMsg());
        return "shangjiaMsg";

    }

    //返回留言记录
    @RequestMapping("showMsg")
    public  String showMsg(Model model){

         List<Msg> list = adminService.showMsg();

         model.addAttribute("list",list);

        return "adminLeaveMsg";
    }
    //删除用户所有信息
    @RequestMapping("deleteUser")
    public String deleteUser(Model model){


        List<User> users = adminService.showUser();
        List<BookOrder> bookOrders = adminService.showOrder();

        model.addAttribute("orders",bookOrders);
        model.addAttribute("users",users);

        return "deleteUser";
    }

    @RequestMapping("deleteU")
    public String deleteU(String userName,Model model){

        String msg = adminService.deleteUser(userName);



        model.addAttribute("msg",msg);



        return "deleteMsg";

    }
    //把订单信息返回到页面




}
